package com.spa.infrastructure.repository.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.CouponCondition;
import com.spa.infrastructure.repository.mapper.CouponMapper;
import com.spa.infrastructure.repository.po.CouponPO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class CouponRepositoryService extends ServiceImpl<CouponMapper, CouponPO> {

    public int insert(CouponPO po) {
        return baseMapper.insert(po);
    }

    public boolean exist(CouponCondition condition) {
       return exists(wrapper(condition));
    }

    public long countByCondition(CouponCondition condition) {
        return count(wrapper(condition));
    }

    public List<CouponPO> listAll(CouponCondition condition){
        LambdaQueryWrapper<CouponPO> wrapper = wrapper(condition);
        return list(wrapper);

    }

    public Page<CouponPO> page(CouponCondition condition){
        LambdaQueryWrapper<CouponPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }

    public boolean updateByCondition(CouponPO po) {
        return update(null, updateWrapper(po));
    }

    private LambdaQueryWrapper<CouponPO> wrapper(CouponCondition condition) {
        LambdaQueryWrapper<CouponPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getUserId()),CouponPO::getUserId,condition.getUserId());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getServiceId()),CouponPO::getServiceId,condition.getServiceId());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getOrderId()),CouponPO::getOrderId,condition.getOrderId());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getStatus()),CouponPO::getStatus,condition.getStatus());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getId()),CouponPO::getId,condition.getId());
        wrapper.eq(StringUtils.isNotEmpty(condition.getName()), CouponPO::getName, condition.getName());
        wrapper.eq(StringUtils.isNotEmpty(condition.getUsername()), CouponPO::getUsername, condition.getUsername());
        wrapper.ge(StringUtils.isNotEmpty(condition.getUseBeginTime()), CouponPO::getUseTime, condition.getUseBeginTime());
        wrapper.le(StringUtils.isNotEmpty(condition.getUseEndTime()), CouponPO::getUseTime, condition.getUseEndTime());
        wrapper.orderByAsc(condition.isStatusAscAndCreateTimeDesc(), CouponPO::getStatus).orderByDesc(condition.isStatusAscAndCreateTimeDesc(), CouponPO::getCreateTime);
        wrapper.orderByDesc(condition.isUseTimeDesc(), CouponPO::getUseTime);
        return wrapper;
    }

    private LambdaUpdateWrapper<CouponPO> updateWrapper(CouponPO po) {
        LambdaUpdateWrapper<CouponPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ObjectUtil.isNotNull(po.getId()), CouponPO::getId, po.getId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getOrderId()), CouponPO::getOrderId, po.getOrderId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getStatus()), CouponPO::getStatus, po.getStatus());
        updateWrapper.set(ObjectUtil.isNotNull(po.getUseTime()), CouponPO::getUseTime, po.getUseTime());
        return updateWrapper;
    }

}
